/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.db.Db;
/*     */ import com.zimbra.cs.db.Db.Error;
/*     */ import com.zimbra.cs.db.DbMailItem;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Collections;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class MailboxErrorUtil
/*     */ {
/*     */   static void handleCascadeFailure(Mailbox mbox, List<Integer> cascadeIds, ServiceException e)
/*     */     throws ServiceException
/*     */   {
/*  40 */     if (causeMatchesFKFailure(e)) {
/*  41 */       ZimbraLog.mailbox.error("deleting cascadeIds failed due to foreign key constraint failed; attempting to delete individually and find failure");
/*  42 */       LinkedList<Integer> failures = new LinkedList();
/*  43 */       for (Integer id : cascadeIds) {
/*     */         try {
/*  45 */           List<Integer> singleItemList = Collections.singletonList(id);
/*  46 */           ZimbraLog.mailbox.debug("attempting to delete id [" + id + "]");
/*  47 */           DbMailItem.delete(mbox, singleItemList, false);
/*  48 */           ZimbraLog.mailbox.debug("deleted [" + id + "] OK");
/*     */         } catch (ServiceException se) {
/*  50 */           ZimbraLog.mailbox.error("deleted FAILED for [" + id + "] due to exception", se);
/*  51 */           failures.add(id);
/*     */         }
/*     */       }
/*  54 */       if (!failures.isEmpty()) {
/*  55 */         StringBuilder sb = new StringBuilder();
/*     */         
/*  57 */         for (Integer id : failures) {
/*  58 */           MailItem item = mbox.getItemById(id.intValue(), MailItem.Type.UNKNOWN);
/*  59 */           String logMsg = "failure item id[" + id + "] type[" + item.getType() + ":" + item.getClass().getSimpleName() + "] subject[" + item.getSubject() + "] size[" + item.getSize() + "] folder[" + item.getFolderId() + "] parent[" + item.getParentId() + "]";
/*     */           
/*     */ 
/*     */ 
/*  63 */           sb.append(logMsg).append("\r\n");
/*  64 */           ZimbraLog.mailbox.error(logMsg);
/*  65 */           Conversation conv; if ((item instanceof Conversation)) {
/*  66 */             conv = (Conversation)item;
/*  67 */             List<Message> children = conv.getMessages();
/*  68 */             if ((children != null) && (children.size() > 0)) {
/*  69 */               ZimbraLog.mailbox.error("converstaion[" + conv.getId() + "] still has " + children.size() + " children.");
/*  70 */               for (Message msg : children) {
/*  71 */                 logMsg = "child[" + msg + "] type[" + msg.getType() + "] subject[" + msg.getSubject() + "] in folder[" + msg.getFolderId() + ":" + msg.getFolder().getName() + "] still associated with conv [" + conv.getId() + "]";
/*     */                 
/*  73 */                 sb.append(logMsg);
/*  74 */                 ZimbraLog.mailbox.error(logMsg);
/*     */               }
/*     */             } } else { Folder folder;
/*  77 */             if ((item instanceof Folder)) {
/*  78 */               folder = (Folder)item;
/*  79 */               List<MailItem.UnderlyingData> children = DbMailItem.getByFolder(folder, MailItem.Type.UNKNOWN, SortBy.NONE);
/*  80 */               if ((children != null) && (children.size() > 0)) {
/*  81 */                 ZimbraLog.mailbox.error("folder[" + folder.getId() + "] still has " + children.size() + " children.");
/*  82 */                 for (MailItem.UnderlyingData data : children) {
/*  83 */                   logMsg = "child[" + item + "] type[" + data.type + "] subject[" + data.getSubject() + "] still present in folder [" + folder.getId() + "]";
/*  84 */                   sb.append(logMsg);
/*  85 */                   ZimbraLog.mailbox.error(logMsg);
/*     */                 }
/*     */               }
/*     */             } else {
/*  89 */               ZimbraLog.mailbox.warn("cascade failure in unexpected type [" + item.getType() + ":" + item.getClass().getSimpleName() + "] other than folder or conversation");
/*     */             }
/*     */           } }
/*  92 */         throw ServiceException.FAILURE(e.getMessage() + "---" + sb.toString(), e);
/*     */       }
/*  94 */       throw ServiceException.FAILURE(e.getMessage() + "--- no additional data available from attempting individual deletes.", e);
/*     */     }
/*     */     
/*  97 */     throw e;
/*     */   }
/*     */   
/*     */   private static boolean causeMatchesFKFailure(Throwable t)
/*     */   {
/* 102 */     if (((t instanceof SQLException)) && (Db.errorMatches((SQLException)t, Db.Error.FOREIGN_KEY_CHILD_EXISTS)))
/* 103 */       return true;
/* 104 */     if (t.getCause() != null) {
/* 105 */       return causeMatchesFKFailure(t.getCause());
/*     */     }
/* 107 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/MailboxErrorUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */